var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const ASSET_PATH = 'js/';
module.exports = {
  entry: {
    index:'./app/index/index.js',
    download:'./app/download/index.js',
    detail:'./app/detail/index.js',
  },
  output: {
    publicPath: ASSET_PATH,
    path: path.resolve(__dirname, 'build/js'),
    // filename: '[name].[hash].js',
    filename: '[name].js?v=[chunkhash]'
  },
  module: {
    loaders: [{
      test: /\.js$/,
      exclude: /node_modules/,
      loader: "babel-loader"
    }, {
      test: /\.html$/,
      // loaders: ['raw-loader']，這個方式也是可以被接受的。
      loader: "raw-loader"
    }, {
      // 此loader配置项的目标是NPM中的jquery
      test: require.resolve('jquery'),
      // 先把jQuery对象声明成为全局变量`jQuery`，再通过管道进一步又声明成为全局变量`$`
      loader: 'expose-loader?$!expose-loader?jQuery'
    }, {
      test: /\.(jpg|png|gif|svg)$/,
      use: 'url-loader'
    }, {
      test: /\.pcss$/,
      use: ExtractTextPlugin.extract({
        fallback: 'style-loader',
        use: ['css-loader', 'postcss-loader']
      })
    }, {
      test: /\.(woff|woff2|eot|ttf|otf)$/,
      use: 'file-loader'
    }]
  },
	plugins: [
	    // 生产环境请注释，否则造成chunkhash报错
      // new webpack.HotModuleReplacementPlugin(),
      new ExtractTextPlugin("[name].css?v=[chunkhash]"),
      new webpack.DefinePlugin({
        'process.env.NODE_ENV': JSON.stringify('production')
      }),
      new webpack.ProvidePlugin({
        $:"jquery",
        jQuery:"jquery",
        'window.jQuery': 'jquery',
        'window.$': 'jquery'
      }),
      new HtmlWebpackPlugin({
          //模版路径根据的是项目根路径
          template: './app/index.html',
          //文件路径根据的是 output.path
          filename:"../index.html",
          //这里要记得引入所有公共模块，Uncaught ReferenceError: webpackJsonp is not defined
          chunks: ['index','common','manifest']
      }),
      new HtmlWebpackPlugin({
          template: './app/download.html',
          filename:"../download.html",
          chunks: ['download','common','manifest']
      }),
      new HtmlWebpackPlugin({
          template: './app/detail.html',
          filename:"../detail.html",
          chunks: ['detail','common','manifest']
      }),
      /**
       * 提供公共代码, 默认会把所有入口节点的公共代码提取出来,生成一个common.js提供公共代码,
       * 默认会把所有入口节点的公共代码提取出来,生成一个common.js.
       * webpack 在使用 CommoChunkPlugin会生成一段 runtime 代码，
       * 而哪怕你不改变common 里的代码这个 runtime 仍然是会跟随着打包变化的并且打入你的common 中，
       * 所以你的common 的hash 就会开始变化了。但是也有办法，就是独立把生成的runtime 代码抽出来。
       **/
      new webpack.optimize.CommonsChunkPlugin({
          name:['common','manifest'],
          minChunks:2
      }),
      //删除之前编译的文件
      new CleanWebpackPlugin(['build']),
      //代码压缩
      new webpack.optimize.UglifyJsPlugin({
        compress: {
            warnings: false,
            drop_console: true,
            drop_debugger: true
        }
      })
    ]
}